//
//  JFDuiHuanController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/8.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "JFDuiHuanController.h"
#import "JFDuiHuanHeadView.h"
#import "JFDuiHuanViewCell.h"
#import "JFDuiHuanFootView.h"
#import "JFDuiHuanModel.h"
#import "BannerModel.h"

#import "JiFenNewsController.h"


@interface JFDuiHuanController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *JiFenCollectView;
@property (strong, nonatomic) JFDuiHuanHeadView * HeadV;
@property (strong, nonatomic) NSMutableArray * BankArr;
@property (strong, nonatomic) NSMutableArray * BannerArr;

@end

@implementation JFDuiHuanController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"积分兑换";
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 0;
    
    self.JiFenCollectView.collectionViewLayout = layout;
    self.JiFenCollectView.delegate = self;
    self.JiFenCollectView.dataSource = self;
    
    [self.JiFenCollectView registerNib:[UINib nibWithNibName:@"JFDuiHuanViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.JiFenCollectView registerNib:[UINib nibWithNibName:@"JFDuiHuanHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.JiFenCollectView registerNib:[UINib nibWithNibName:@"JFDuiHuanFootView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    [self UpLoadDataOFJFDuiHuanController];
    
    [self UpLoadDataOfBannerView];
}

- (void)UpLoadDataOfBannerView{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"5" forKey:@"type"];
    [dic setObject:KUseriPhone forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"plug" andAction:@"Server" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"返回的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0 ; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                BannerModel * model = [BannerModel mj_objectWithKeyValues:dic];
                [self.BannerArr addObject:model];
                
            }
            [self.HeadV AddBannerImage:self.BannerArr];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
}

- (void)UpLoadDataOFJFDuiHuanController{
    [MainObjectWay PostWithMethod:@"bank_list" andAction:@"Points" andAddParam:NO parameters:nil success:^(id responseObject) {
        NSLog(@"积分兑换请求数据 = %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i =0 ; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                JFDuiHuanModel * model = [JFDuiHuanModel mj_objectWithKeyValues:dic];
                [self.BankArr addObject:model];
            }
            [self.JiFenCollectView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息= %@",error);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.BankArr.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat W = (KScreenW-30)/4;
    return CGSizeMake(W, 70);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JFDuiHuanViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    JFDuiHuanModel * model = self.BankArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JFDuiHuanModel * model = self.BankArr[indexPath.row];
    JiFenNewsController * JFNews = [[JiFenNewsController alloc] init];
    JFNews.BankID = model.bank_id;
    [self.navigationController pushViewController:JFNews animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreenW, 200);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(KScreenW, 90);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        self.HeadV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        [self.HeadV CreateOneControllerHead01];
        return self.HeadV;
    }else{
        JFDuiHuanFootView * footV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
        return footV;
    }
}

- (NSMutableArray *)BankArr{
    if (!_BankArr) {
        _BankArr = [NSMutableArray array];
    }
    return _BankArr;
}

- (NSMutableArray *)BannerArr {
    if (!_BannerArr) {
        _BannerArr = [NSMutableArray array];
    }
    return _BannerArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
